package com.sunny.cloudeurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

@RestController
@EnableEurekaClient
@SpringBootApplication
public class CloudEurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudEurekaClientApplication.class, args);
    }

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "sunny") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

    AtomicInteger ac = new AtomicInteger();

    /**
     * 用于重试
     */
    @GetMapping("/data")
    public String getData() {
        ac.addAndGet(1);
        if (ac.get() <= 4) {
            System.err.println("retry num: " + ac.get());
            throw new RuntimeException("error");
        }
        String result = "retry num: " + ac.get();
        ac.set(0);
        return result;
    }
}
